package com.haonan.controller;

import com.haonan.pojo.entily.Result;
import com.haonan.Utils.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class uploadController {
    /**
    本地磁盘存储数据
     */
    /* *  @PostMapping("/upload")
   public Result upload(String name, Integer age, MultipartFile file) throws IOException {
       //获取文件原始名称
        String fileName = file.getOriginalFilename();
        //截取文件名的后缀
        String last=fileName.substring(fileName.lastIndexOf("."));
        //拼接新的文件名
        String newFileName = UUID.randomUUID().toString()+last;
        //本地存放，存放在D盘image文件夹下
        file.transferTo(new File("D:/image/"+newFileName));
       return Result.success();
   }*/
    /**
    阿里云OSS存储数据
     */
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
     public Result upload( MultipartFile file) throws Exception {
        String url= aliyunOSSOperator.upload(file.getBytes(),file.getOriginalFilename());
        return Result.success(url);
    }
}
